package com.ui.controls
{
	import com.ui.core.UIComponent;
	import com.ui.data.ToolTipData;
	import com.ui.manager.UIManager;

	import flash.display.DisplayObject;

	/**
	 * Game ToolTip
	 * 
	 * @author Cafe
	 * @version 20100719
	 */
	public class ToolTip extends UIComponent
	{
		protected var _data:ToolTipData;
		protected var _backgroundSkin:DisplayObject;
		protected var _label:Label;

		override protected function create():void
		{
			if (_data.backgroundAsset)
			{
				_backgroundSkin = UIManager.getUI( _data.backgroundAsset );
				addChild( _backgroundSkin );
				_label = new Label( _data.labelData );
				_label.x = _label.y = _data.padding;
				addChild( _label );
			}
		}

		override protected function layout():void
		{
			if (_data.backgroundAsset)
			{
				_width = _label.width + _data.padding * 2;
				_height = _label.height + _data.padding * 2;
				_backgroundSkin.width = _width;
				_backgroundSkin.height = _height;
			}
		}

		public function ToolTip( data:ToolTipData )
		{
			_data = data;
			super( data );
		}

		public function get data():ToolTipData
		{
			return _data;
		}

		public function get text():String
		{
			return _label.text;
		}

		public function get label():Label
		{
			return _label;
		}

		override public function set source( value:* ):void
		{
			if (_source != value)
			{
				if (value == null)
				{
					_label.clear();
				}
				else
				{
					if (_label.htmlText != value)
					{
						_label.htmlText = String( value );
						layout();
					}
				}
				_source = value;
			}
			_data.source = value;
		}
	}
}